package gentle.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import gentle.service.ICustomerService;
import gentle.entity.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import gentle.mapper.CustomerDao;

@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerDao dao;

    @Override
    public List<CustomerInfo> selectPageListByQueryGetMap(Page<CustomerInfo> page, SearchCustomerInfo searchInfo) {
        String tableHead = searchInfo.getAbbreviation();
        String table1 = tableHead + "deal_info";
        String table2 = tableHead + "customer_info";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtils.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtils.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtils.isNotEmpty(searchInfo.getCondition())) {
            params.put("condition", "");
        } else {
            params.put("condition", "t.arrival_status!=8");
        }
        if (StringUtils.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtils.isNotEmpty(searchInfo.getTreatmentUnit())) {
            params.put("treatmentUnit", searchInfo.getTreatmentUnit());
        }
        if (StringUtils.isNotEmpty(searchInfo.getDealStatus())) {
            if ("已结账".equals(searchInfo.getDealStatus())) {
                params.put("dealStatus", "t.deal_status ='已结账'");
            } else {
                params.put("dealStatus", "t.deal_status !='已结账'");
            }
        }
        if (StringUtils.isNotEmpty(searchInfo.getStartArrivalTime())) {
            params.put("startArrivalTime", searchInfo.getStartArrivalTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndArrivalTime())) {
            params.put("endArrivalTime", searchInfo.getEndArrivalTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getReservations())) {
            params.put("reservations", searchInfo.getReservations());
        }
        if (StringUtils.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }

        if (StringUtils.isNotEmpty(searchInfo.getStartUpdateTime()) && StringUtils.isEmpty(searchInfo.getStartAddTime())) {
            params.put("startUpdateTime", searchInfo.getStartUpdateTime());
            params.put("gbtk", "group by tk.reservations_id");
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndUpdateTime())) {
            params.put("endUpdateTime", searchInfo.getEndUpdateTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getStartAddTime()) && StringUtils.isEmpty(searchInfo.getStartUpdateTime())) {
            params.put("startAddTime", searchInfo.getStartAddTime());
            params.put("gbtk", "group by tk.reservations_id");
        }
        if (StringUtils.isEmpty(searchInfo.getStartAddTime()) && StringUtils.isEmpty(searchInfo.getStartUpdateTime())) {
            params.put("gbtk", "and t.arrival_status!=1 and t.arrival_status!=9 group by t.id");
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndAddTime())) {
            params.put("endAddTime", searchInfo.getEndAddTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtils.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtils.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());
        }
        if (StringUtils.isNotEmpty(searchInfo.getOppStatus())) {
            if ("1".equals(searchInfo.getOppStatus())) {
                params.put("oppStatus", "tk.open_id is not null");
            } else if ("2".equals(searchInfo.getOppStatus())) {
                params.put("oppStatus", "tk.open_id is null");
            } else {
                params.put("", "");
            }
        }
        if (StringUtils.isNotEmpty(searchInfo.getOneChannel())) {
            if (searchInfo.getOneChannel().indexOf("请选择") == -1) {
                String oneChannel = "";
                String oneChannel2[] = searchInfo.getOneChannel().split(",");
                if (oneChannel2.length == 1) {
                    oneChannel = oneChannel2[0];
                } else {
                    for (int i = 0; i < oneChannel2.length; i++) {
                        if (i == (oneChannel2.length - 1)) {
                            oneChannel += "'" + oneChannel2[i];
                        } else if (i == 0) {
                            oneChannel += oneChannel2[i] + "'" + ",";
                        } else {
                            oneChannel += "'" + oneChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("oneChannel", " t.one_channel in ('" + oneChannel + "')");
            }
        }
        if (StringUtils.isNotEmpty(searchInfo.getTwoChannel())) {
            if (searchInfo.getTwoChannel().indexOf("请选择") == -1) {
                String twoChannel = "";
                String twoChannel2[] = searchInfo.getTwoChannel().split(",");
                if (twoChannel2.length == 1) {
                    twoChannel = twoChannel2[0];
                } else {
                    for (int i = 0; i < twoChannel2.length; i++) {
                        if (i == (twoChannel2.length - 1)) {
                            twoChannel += "'" + twoChannel2[i];
                        } else if (i == 0) {
                            twoChannel += twoChannel2[i] + "'" + ",";
                        } else {
                            twoChannel += "'" + twoChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("twoChannel", " t.two_channel in ('" + twoChannel + "')");
            }
        }
        if (StringUtils.isNotEmpty(searchInfo.getThrChannel())) {
            if (searchInfo.getThrChannel().indexOf("请选择") == -1) {
                String thrChannel = "";
                String thrChannel2[] = searchInfo.getThrChannel().split(",");
                if (thrChannel2.length == 1) {
                    thrChannel = thrChannel2[0];
                } else {
                    for (int i = 0; i < thrChannel2.length; i++) {
                        if (i == (thrChannel2.length - 1)) {
                            thrChannel += "'" + thrChannel2[i];
                        } else if (i == 0) {
                            thrChannel += thrChannel2[i] + "'" + ",";
                        } else {
                            thrChannel += "'" + thrChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("thrChannel", " t.thr_channel in ('" + thrChannel + "')");
            }
        }
        params.put("tableName", table2);
        params.put("tableHead", "tk.abbreviation ='" + tableHead + "'");
        params.put("table1", table1);
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPage(page, params);
    }

    public int getQueryPagesCount(Page<CustomerInfo> page, SearchCustomerInfo searchInfo) {
        String tableHead = searchInfo.getAbbreviation();
        String table1 = tableHead + "deal_info";
        String table2 = tableHead + "customer_info";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtils.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtils.isNotEmpty(searchInfo.getCondition())) {
            params.put("condition", "");
        } else {
            params.put("condition", "t.arrival_status!=8");
        }
        if (StringUtils.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtils.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtils.isNotEmpty(searchInfo.getTreatmentUnit())) {
            params.put("treatmentUnit", searchInfo.getTreatmentUnit());
        }
        if (StringUtils.isNotEmpty(searchInfo.getDealStatus())) {
            if ("已结账".equals(searchInfo.getDealStatus())) {
                params.put("dealStatus", "t.deal_status ='已结账'");
            } else {
                params.put("dealStatus", "t.deal_status !='已结账'");
            }
        }
        if (StringUtils.isNotEmpty(searchInfo.getStartArrivalTime())) {
            params.put("startArrivalTime", searchInfo.getStartArrivalTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndArrivalTime())) {
            params.put("endArrivalTime", searchInfo.getEndArrivalTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getReservations())) {
            params.put("reservations", searchInfo.getReservations());
        }
        if (StringUtils.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getStartUpdateTime()) && StringUtils.isEmpty(searchInfo.getStartAddTime())) {
            params.put("startUpdateTime", searchInfo.getStartUpdateTime());
            params.put("gbtk", "group by tk.reservations_id");
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndUpdateTime())) {
            params.put("endUpdateTime", searchInfo.getEndUpdateTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getStartAddTime()) && StringUtils.isEmpty(searchInfo.getStartUpdateTime())) {
            params.put("startAddTime", searchInfo.getStartAddTime());
            params.put("gbtk", "group by tk.reservations_id");
        }
        if (StringUtils.isEmpty(searchInfo.getStartAddTime()) && StringUtils.isEmpty(searchInfo.getStartUpdateTime())) {
            params.put("gbtk", "and t.arrival_status!=1 and t.arrival_status!=9 group by t.id");
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndAddTime())) {
            params.put("endAddTime", searchInfo.getEndAddTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtils.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtils.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());
        }
        if (StringUtils.isNotEmpty(searchInfo.getOppStatus())) {
            if ("1".equals(searchInfo.getOppStatus())) {
                params.put("oppStatus", "tk.open_id is not null");
            } else if ("2".equals(searchInfo.getOppStatus())) {
                params.put("oppStatus", "tk.open_id is null");
            } else {
                params.put("", "");
            }
        }
        if (StringUtils.isNotEmpty(searchInfo.getOneChannel())) {
            if (searchInfo.getOneChannel().indexOf("请选择") == -1) {
                String oneChannel = "";
                String oneChannel2[] = searchInfo.getOneChannel().split(",");
                if (oneChannel2.length == 1) {
                    oneChannel = oneChannel2[0];
                } else {
                    for (int i = 0; i < oneChannel2.length; i++) {
                        if (i == (oneChannel2.length - 1)) {
                            oneChannel += "'" + oneChannel2[i];
                        } else if (i == 0) {
                            oneChannel += oneChannel2[i] + "'" + ",";
                        } else {
                            oneChannel += "'" + oneChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("oneChannel", " t.one_channel in ('" + oneChannel + "')");
            }
        }
        if (StringUtils.isNotEmpty(searchInfo.getTwoChannel())) {
            if (searchInfo.getTwoChannel().indexOf("请选择") == -1) {
                String twoChannel = "";
                String twoChannel2[] = searchInfo.getTwoChannel().split(",");
                if (twoChannel2.length == 1) {
                    twoChannel = twoChannel2[0];
                } else {
                    for (int i = 0; i < twoChannel2.length; i++) {
                        if (i == (twoChannel2.length - 1)) {
                            twoChannel += "'" + twoChannel2[i];
                        } else if (i == 0) {
                            twoChannel += twoChannel2[i] + "'" + ",";
                        } else {
                            twoChannel += "'" + twoChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("twoChannel", " t.two_channel in ('" + twoChannel + "')");
            }
        }
        if (StringUtils.isNotEmpty(searchInfo.getThrChannel())) {
            if (searchInfo.getThrChannel().indexOf("请选择") == -1) {
                String thrChannel = "";
                String thrChannel2[] = searchInfo.getThrChannel().split(",");
                if (thrChannel2.length == 1) {
                    thrChannel = thrChannel2[0];
                } else {
                    for (int i = 0; i < thrChannel2.length; i++) {
                        if (i == (thrChannel2.length - 1)) {
                            thrChannel += "'" + thrChannel2[i];
                        } else if (i == 0) {
                            thrChannel += thrChannel2[i] + "'" + ",";
                        } else {
                            thrChannel += "'" + thrChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("thrChannel", " t.thr_channel in ('" + thrChannel + "')");
            }
        }
        params.put("tableName", table2);
        params.put("tableHead", "tk.abbreviation ='" + tableHead + "'");
        params.put("table1", table1);
        return dao.getQueryPagesCount(page, params);
    }
}